package illusion

import (
	"github.com/jinzhu/gorm"
	"sync"
	"fmt"
	"os"
)

type Model struct {
	gorm.Model
}

var m *gorm.DB
var _once sync.Once

func Manager() *gorm.DB{
	_once.Do(func(){
		option := dbConfig()
		if option.Driver == "mysql"{ //只有为mysql时才添加orm支持
			dsn := option.User + ":" + option.Password+"@tcp(" + option.Host + ":" + option.Port + ")/" + option.Database + "?charset=utf8&loc=Local&parseTime=true"
			Logger().Println("数据库连接dsn:" + dsn)
			if is_debug {
				fmt.Println("数据库连接dsn:" + dsn)
			}
			var err error
			m,err = gorm.Open("mysql", dsn)
			if err != nil {
				logging(err.Error())
				os.Exit(0)
				//panic(err.Error())
			}
			m.SingularTable(true)
		}else {
			Logger().Println("driver:" + option.Driver)
			if is_debug {
				fmt.Println("driver:" + option.Driver)
			}
			panic("暂不支持除了mysql之外的数据库连接")
		}

	})
	return m
}

